package Leo.fileIO.impl;

import Leo.fileIO.FileUpload;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;

@Service("fileUpload")
public class FileUploadImpl implements FileUpload{
    public long uploadFile(MultipartFile file, String path,String suffix) throws IOException {
        long start = System.currentTimeMillis();
        System.out.println("start trans:" + start);
        File newFile = new File(path);
        if(!newFile.exists()){
            newFile.mkdirs();
        }
        long currentTimeMillis = System.currentTimeMillis();
        File realFile = new File(path , currentTimeMillis + "." + suffix);
//        if(!realFile.exists()){
//            realFile.createNewFile();
//        }
        file.transferTo(realFile);
        long end = System.currentTimeMillis();
        long l = end - start;
        System.out.println("end trans:" + end);
        System.out.println(end - start);
        return l;
    }

    public long uploadFile(List<MultipartFile> files, String path) throws IOException {
        long start = System.currentTimeMillis();
        System.out.println("start trans:" + start);
        File newFile = new File(path);
        if(!newFile.exists()){
            newFile.mkdirs();
        }
        for (MultipartFile file : files) {
            long currentTimeMillis = System.currentTimeMillis();
            File realFile = new File(path , file.getOriginalFilename());
//        if(!realFile.exists()){
//            realFile.createNewFile();
//        }
            file.transferTo(realFile);

        }
        long end = System.currentTimeMillis();
        long l = end - start;
        System.out.println("end trans:" + end);
        System.out.println(end - start);
        return l;
    }
}
